package com.nineclock.sign.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nineclock.sign.mapper.SignMapper;
import com.nineclock.sign.service.SignlzzService;
import com.nineclock.common.enums.ResponseEnum;
import com.nineclock.common.exception.NcException;
import com.nineclock.common.utils.BeanHelper;
import com.nineclock.sign.dto.SignAllUserDTO;
import com.nineclock.sign.dto.SignDTO;
import com.nineclock.sign.pojo.SignInfo;
import com.nineclock.system.dto.SysCompanyUserDTO;
import com.nineclock.system.feign.OrganizationFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SignlzzServiceImpl implements SignlzzService {
    @Autowired
    SignMapper signMapper;
    @Autowired
    OrganizationFeign organizationFeign;
    @Override
    public List<SignAllUserDTO> listSignAllUser(String dateStr) {
        if (dateStr == null) {
            throw new NcException(ResponseEnum.INVALID_PARAM_ERROR);
        }
        List<SignAllUserDTO>data=new ArrayList<>();
        List<SysCompanyUserDTO> allCompanyUser = organizationFeign.queryAllCompanyUser().getData();
        if (allCompanyUser!=null){
            for (SysCompanyUserDTO sysCompanyUserDTO : allCompanyUser) {
            LambdaQueryWrapper<SignInfo> signInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
            signInfoLambdaQueryWrapper.eq(SignInfo::getCompanyId, sysCompanyUserDTO.getCompanyId());
            signInfoLambdaQueryWrapper.eq(SignInfo::getCreateDate,dateStr);
            signInfoLambdaQueryWrapper.eq(SignInfo::getCompanyUserId,sysCompanyUserDTO.getId());
            List<SignInfo> signInfos = signMapper.selectList(signInfoLambdaQueryWrapper);
            List<SignDTO>signDTOS=new ArrayList<>();
            if (signInfos!=null){
                for (SignInfo signInfo : signInfos) {
                    SignDTO signDTO = BeanHelper.copyProperties(signInfo, SignDTO.class);
                    signDTO.setDayCount(signInfos.size()+"");
                    signDTOS.add(signDTO);
                }
            }
            SignAllUserDTO signAllUserDTO=new SignAllUserDTO();
            signAllUserDTO.setSignResponseList(signDTOS);
            signAllUserDTO.setUsername(sysCompanyUserDTO.getUserName());
            data.add(signAllUserDTO);

        }}






        return data;
    }
}
